from django.views import View
from django.utils.decorators import method_decorator

import json
import hashlib
from utils import re_valid
from .models import UserProfile
from utils.return_data import resultError, resultSuccess
from utils.logging_check import logging_check


def upload_avatar(request, username):
    if request.method == 'GET':
        return resultError('111', '请使用post请求方式')
    elif request.method == 'POST':
        try:
            user = UserProfile.objects.get(username=username)
            avatar = request.FILES['avatar']
            user.avatar = avatar
            user.save()
            return resultSuccess('200', '图片上传成功')
        except Exception as e:
            return resultError('111', '用户名称错误')


class UserViews(View):

    def get(self, request, username=None):
        user = UserProfile.objects.filter(username=username)

        if len(user):
            user = user[0]
            result_data = {
                'username': user.username,
                'info': user.info,
                'sign': user.sign,
                'nickname': user.nickname,
                'avatar': str(user.avatar)
            }

            return resultSuccess('200', '用户信息', result_data)
        else:
            return resultError('111', '用户名称错误')

    def post(self, request):
        json_str = request.body
        json_obj = json.loads(json_str)

        username = json_obj['username']
        email = json_obj['email']
        password_1 = json_obj['password_1']
        password_2 = json_obj['password_2']
        phone = json_obj['phone']

        # 判断各个字段是否为空
        isNull = self.judgeNull(username, email, password_1, password_2, phone)
        if isNull:
            return isNull

        # 正则校验
        # 邮箱
        if not re_valid.email_valid(email):
            return resultError('111', '请输入正确的邮箱格式')

        # 手机号
        if not re_valid.phone_valid(phone):
            return resultError('111', '手机号格式不正确')

        # 密码比对
        if password_2 != password_1:
            return resultError('111', '两次密码不一致')

        # 判断用户是否存在
        user = UserProfile.objects.filter(username=username)
        if len(user) > 0:
            return resultError('222', '该用户名已被注册')

        # 插入用户信息
        # 密码进行md5加密
        password_hash_md5 = hashlib.md5()
        password_hash_md5.update(password_1.encode())
        UserProfile.objects.create(
            username=username,
            nickname=username,
            password=password_hash_md5.hexdigest(),
            email=email,
            phone=phone
        )

        return resultSuccess('200', '注册成功', '用户信息')

    @method_decorator(logging_check)
    def put(self, request, username=None):
        user = UserProfile.objects.filter(username=username)

        if len(user) == 1:
            user = user[0]

            json_str = request.body
            json_obj = json.loads(json_str)

            nickname = json_obj['nickname']
            sign = json_obj['sign']
            info = json_obj['info']

            user.nickname = nickname
            user.sign = sign
            user.info = info
            user.save()

            return resultSuccess('200', '信息更新成功')
        else:
            return resultError('111', '查询用户错误')

    # 注册, 判断各个字段是否为空
    def judgeNull(self, username, email, password_1, password_2, phone):
        if username == '' or username is None:
            return resultError('000', '用户名不能为空')

        if email == '' or email is None:
            return resultError('000', '邮箱不能为空')

        if phone == '' or phone is None:
            return resultError('000', '手机号不能为空')

        if password_1 == '' or password_1 is None:
            return resultError('000', '密码不能为空')

        if password_2 == '' or password_2 is None:
            return resultError('000', '请再次输入密码')

        return None
